<?php
if( !defined( 'EXCEL' ) ): define( 'EXCEL', 1 );
if( !defined( 'FILE_ROOT' ) ) include_once( FILE_ROOT . 'libcof.class.php' );

/**
 * GESTION D'ENVOIS D'EMAILS
 */
USE_LIB( 'template' );
require_once( LIB_ROOT . 'excel/class.writeexcel_workbook.inc.php' );
require_once( LIB_ROOT . 'excel/class.writeexcel_worksheet.inc.php' );

class Excel extends Template
	{
	private $m_workbook;
	private $m_worksheets;
	private $m_formats;
	public $m_filename;
	private $m_current_worksheet;
	private $m_column_types;
	private $m_column_formats;
	private $m_format;
	
	public function __construct( $filename )
		{
		$this->m_filename = $filename;
		$this->m_workbook = new writeexcel_workbook( $filename );
		$this->m_worksheets = array();
		$this->m_formats = array();
		$this->m_current_worksheet = null;
		$this->m_column_types = null;
		$this->m_column_formats = null;
		}
		
	public function addWorksheet( $title, $name = null )
		{
		$this->m_current_worksheet =& $this->m_workbook->addworksheet($title);
		$this->m_row = 0;
		$this->m_col = 0;
		}

	public function setWorksheet( $title )
		{
		$this->m_current_worksheet =& $this->m_workbook->addworksheet($title);
		$this->m_row = 0;
		$this->m_col = 0;
		}
		
	public function addFormat( $name, $size, $color="black", $is_bold=false )
		{
		$this->m_formats[$name] =& $this->m_workbook->addformat(array(
			'size' => $size,
			'color' => $color,
			'bold' => ($is_bold)?1:0
			));
		}
		
	public function setFormat( $size, $color="black", $is_bold=false )
		{
		$this->m_format = $this->m_workbook->addformat(array(
			'size' => $size,
			'color' => $color,
			'font_charset' => 'utf-8',
			'bold' => ($is_bold)?1:0
			));
		}
		
	public function setDefaultFormat( $size, $color="black", $is_bold=false )
		{
		$this->addFormat( 'default', $size, $color, $is_bold );
		}
		
	public function writeLn( $text, $format = null, $type = null )
		{
		$this->write( $text, $format, $type );
		$this->newLine();
		}
		
	public function write( $text, $format = null, $type = null )
		{
		if( isset($this->m_column_types) && isset( $this->m_column_types[$this->m_col] ) )
			$type = isset( $type ) ? $type : $this->m_column_types[$this->m_col];
		else
			$type = isset( $type ) ? $type : 'text';
		$text = $text;
		switch( $type )
			{
			case 'number':
				$this->m_current_worksheet->write_number($this->m_row, $this->m_col,  $text, $this->m_format);
				break;
			case 'url':
				$url = $name = $text;
				if( stripos($text, '#') !== false )
				list( $url, $name ) = explode( '#', $text );
				$this->m_current_worksheet->write_url($this->m_row, $this->m_col, $url, $name, $this->m_format);
				break;
			case 'telephone':
				$text = preg_replace('@[^0-9]@','',$text);
				if( strlen($text)==9 ) $text = '0' . $text;
				$this->m_current_worksheet->write_string($this->m_row, $this->m_col,  $text, $this->m_format);
				break;
			case 'date':
				$text = date('j/m/y H:i',$text);
			default:
				$this->m_current_worksheet->write_string($this->m_row, $this->m_col,  $text, $this->m_format);
			}

		$this->m_col++;
		}
		
	public function writeArray( $array, $format = null )
		{
		$end = count( $array );
		for( $i=0; $i<$end; $i++ )
			{
			$this->write( ARRAYVAL($array,$i), $format );
			}
		$this->newLine();
		}
		
	public function newLine()
		{
		$this->m_col=0;
		$this->m_row++;
		}
		
	public function setColumnTypes( $array_of_types )
		{
		$this->m_column_types = $array_of_types;
		}
		
	public function clearColumnTypes()
		{
		unset( $this->m_column_types );
		}
		
	public function setColumnFormats( $array_of_formats )
		{
		$this->m_column_formats = $array_of_formats;
		}
		
	public function clearColumnFormats()
		{
		unset( $this->m_column_formats );
		}
	
	public function save()
		{
		$this->m_workbook->close();
		}
		
	public function forceDownload( $filename )
		{
		$this->save();
		$html = file_get_contents($this->m_filename);
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: private",false);
		header("Content-Type: application/x-msexcel");
		header("Content-Disposition: attachment; filename=\"$filename\";" );
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: ".strlen($html));
		echo $html;
		unlink($this->m_filename);
		exit();
		}
	}
endif;
?>